﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.IO;

namespace XDesigner.Development.Dom
{
    /// <summary>
    /// 解决方案文件
    /// </summary>
    [Serializable]
    public class Solution
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public Solution()
        {
        }

        private string _Name = null;
        /// <summary>
        /// 对象名称
        /// </summary>
        [DefaultValue(null)]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _FileName = null;
        /// <summary>
        /// 文件名
        /// </summary>
        [DefaultValue(null)]
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        private string _Version = null;
        /// <summary>
        /// 版本号
        /// </summary>
        public string Version
        {
            get { return _Version; }
            set { _Version = value; }
        }

        private ProjectElementList _Projects = new ProjectElementList();

        /// <summary>
        /// 工程列表
        /// </summary>
        [XmlArrayItem("Project", typeof(ProjectElement))]
        public ProjectElementList Projects
        {
            get { return _Projects; }
            set { _Projects = value; }
        }

        private FileElementList _Files = new FileElementList();
        /// <summary>
        /// 附件的文件
        /// </summary>
        [XmlArrayItem("File", typeof(FileElement))]
        public FileElementList Files
        {
            get { return _Files; }
            set { _Files = value; }
        }

        private SolutionStyle _Style = SolutionStyle.VSSolution;
        /// <summary>
        /// 解决方案类型
        /// </summary>
        [DefaultValue(SolutionStyle.VSSolution)]
        public SolutionStyle Style
        {
            get { return _Style; }
            set { _Style = value; }
        }

        public bool Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            if (System.IO.File.Exists(fileName) == false)
            {
                throw new System.IO.FileNotFoundException(fileName);
            }
            this.Projects.Clear();
            string ext = System.IO.Path.GetExtension(fileName);
            if (ext == null)
            {
                return false;
            }
            ext = ext.Trim().ToLower();
            if (ext == ".sln")
            {
                this.Style = SolutionStyle.VSSolution;
                VSNETHelper.LoadSolution(fileName, this);
                return true;
            }
            else if (ext == ".vbg")
            {
                // not support currently
                return false;
            }
            else if (ext == ".dsw")
            {
                this.Style = SolutionStyle.VCWorkspace;
                return VSNETHelper.LoadDSW(this, fileName);
            }
            else if (ext == ".vbp")
            {
                this.Style = SolutionStyle.VBGroup;
                ProjectElement prj = new ProjectElement();
                VSNETHelper.LoadVBProject(prj, fileName);
                this.Projects.Add(prj);
                this.Name = System.IO.Path.GetFileNameWithoutExtension(fileName);
                this.FileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".vbg";
                return true ;
            }
            else if (ext == ".csproj" || ext == ".csproj" || ext == ".vcproj")
            {
                this.Style = SolutionStyle.VSSolution;
                this.Projects.Add(VSNETHelper.LoadProject(fileName));
                this.Name = System.IO.Path.GetFileNameWithoutExtension(fileName);
                this.FileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".sln";
                return true;
            }
            else if (ext == ".dsp")
            {
                this.Style = SolutionStyle.VCWorkspace;
                ProjectElement prj = new ProjectElement();
                VSNETHelper.LoadDSP(prj, fileName);
                this.Projects.Add(prj);
                this.Name = System.IO.Path.GetFileNameWithoutExtension(fileName);
                this.FileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + ".dsw";
                return true;
            }
            else if (ext == ".bdsproj")
            {
                return DelphiHelper.LoadSolutionFile(this, fileName); 
            }
            else if (ext == ".dpr")
            {
                this.Style = SolutionStyle.Delphi;
                ProjectElement prj = new ProjectElement();
                if (DelphiHelper.LoadProject(prj, fileName))
                {
                    this.Projects.Add(prj);
                    this.Name = Path.GetFileNameWithoutExtension(fileName);
                    this.FileName = Path.GetFileNameWithoutExtension(fileName) + ".bdsproj";
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    }
    /// <summary>
    /// 解决方案类型
    /// </summary>
    public enum SolutionStyle
    {
        VBGroup,
        VSSolution,
        Delphi,
        VCWorkspace
    }
}
